package com.itrencai.d13_thread;

public class Account {
    private  String cardId;
    private  double money;//账户余额

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public double getMoney() {
        return money;
    }

    public Account() {
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public Account(String cardId, double money) {
        this.cardId = cardId;
        this.money = money;
    }

    //小明小红取钱
    public void drawMoney(double money) {
        //先了解是谁来取钱，线程的名字就是人名
        String name =Thread.currentThread().getName();
        //1.判断账户是否够钱
        //同步代码块
        synchronized ("uoou") {//锁
            if(this.money>=money){
                //2.取钱
                System.out.println(name+"来取钱成功，吐出："+money);
                //3.剩余金额
                this.money-=money;
                System.out.println("取钱后剩余："+this.money);
            }else{
                //余额不足
                System.out.println(name+"来取钱，余额不足");
            }
        }

    }
    @Override
    public String toString() {
        return "Account{" +
                "cardId='" + cardId + '\'' +
                ", money=" + money +
                '}';
    }


}
